package org.ehe.business.project.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ehe.business.monitor.MonitorMethod;
import org.ehe.business.overdue.extract.SolutionDataExtractor;
import org.ehe.business.project.domain.ProjectSolution;
import org.ehe.business.project.domain.bo.ProjectSolutionBo;
import org.ehe.business.project.domain.vo.ProjectSolutionVO;
import org.ehe.business.project.service.ProjectSolutionService;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.bind.annotation.*;

/**
 * 方案管理
 * @author :zhangnn
 * @className :ProjectSolutionController
 * @description: TODO
 * @date 2025-07-25 11:54:11
 */
@Slf4j
@RestController
@RequestMapping("/solution")
@RequiredArgsConstructor
public class ProjectSolutionController extends BaseController {

    private final ProjectSolutionService solutionService;

    /**
     * 列表
     */
    @PostMapping("/page")
    public TableDataInfo<ProjectSolutionVO> pageSolutions(@RequestBody ProjectSolutionBo bo, PageQuery pageQuery) {
        return solutionService.pageSolutions(bo, pageQuery);
    }

    /**
     * 新增项目
     *
     * @param request 请求参数
     * @return
     */
    @SaCheckPermission("project:solution:add")
    @MonitorMethod(bizType = "solutionOverdue", extractor = SolutionDataExtractor.class)
    @Log(title = "项目方案新增", linkType = "project", businessType = BusinessType.INSERT, idField = "solutionName", nameField = "solutionName")
    @PostMapping("/create")
    public R createProjectSolution(@RequestBody ProjectSolution request) {
        try {
            int result = solutionService.createProjectSoution(request);
            if (result > 0) {
                return R.ok("方案创建成功");
            } else {
                return R.fail("方案创建失败");
            }
        } catch (RuntimeException e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 修改
     * @param request
     * @return
     */
    @SaCheckPermission("project:solution:edit")
    @MonitorMethod(bizType = "solutionOverdue", extractor = SolutionDataExtractor.class)
    @Log(title = "项目方案修改", linkType = "project", businessType = BusinessType.UPDATE, idField = "solutionName", nameField = "solutionName")
    @PostMapping("/update")
    public R updateProjectSolution(@RequestBody ProjectSolution request) {
        try {
            int result = solutionService.updateProjectSolution(request);
            if (result > 0) {
                return R.ok("方案更新成功");
            } else {
                return R.fail("方案更新失败");
            }
        } catch (RuntimeException e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 查看项目方案详情
     * @param id
     * @return
     */
    @SaCheckPermission("project:solution:view")
    @PostMapping("/view/{id}")
    public R<ProjectSolutionVO> viewProjectSolution(@PathVariable("id") Long id) {
        return R.ok(solutionService.viewProjectSolution(id));
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @SaCheckPermission("project:solution:remove")
    @Log(title = "项目方案删除", linkType = "project", businessType = BusinessType.DELETE, idField = "solutionName", nameField = "solutionName")
    @PostMapping("/delete/{id}")
    public R deleteProjectSolution(@PathVariable("id") Long id) {
        boolean b = solutionService.deleteProjectSolution(id);
        if(!b){
           return R.fail("删除设计方案失败");
        }
        return R.ok("删除设计方案成功！");

    }


    /**
     * 作废项目方案
     * 只允许草稿状态或未通过状态的方案作废
     * @param id 方案ID
     * @return
     */
    @Log(title = "项目方案作废", linkType = "solution", businessType = BusinessType.UPDATE, idField = "solutionName", nameField = "solutionName")
    @PostMapping("/{id}/invalidate")
    public R<Void> invalidateSolution(@PathVariable Long id) {
        try {
            int result = solutionService.invalidateProjectSolution(id);
            if (result > 0) {
                return R.ok("方案作废成功");
            } else {
                return R.fail("方案作废失败");
            }
        } catch (RuntimeException e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 审核拒绝
     * 项目方案审批
     */
    @MonitorMethod(bizType = "solutionOverdue", extractor = SolutionDataExtractor.class)
    @Log(title = "项目方案审批-驳回", linkType = "project", businessType = BusinessType.REJECT, idField = "solutionName", nameField = "solutionName")
    @PostMapping("/reject/{solutionId}/{auditUser}")
    public R<ProjectSolution> rejectSolution(@PathVariable Long solutionId,
                                             @PathVariable String auditUser) {
        try {
            ProjectSolution solution = solutionService.rejectSolution(solutionId, auditUser);
            return R.ok("审核拒绝成功，已启动超期监控",solution);
        } catch (Exception e) {
            log.error("审核拒绝失败: {}", e.getMessage(), e);
            return R.fail("审核拒绝失败: " + e.getMessage());
        }
    }

    /**
     * 提交
     */
    @SaCheckPermission("project:solution:submit")
    @MonitorMethod(bizType = "solutionOverdue", extractor = SolutionDataExtractor.class)
    @Log(title = "项目方案提交", linkType = "project", businessType = BusinessType.INSERT, idField = "solutionName", nameField = "solutionName")
    @PostMapping("/submit/{solutionId}/{days}")
    public R<ProjectSolution> resubmitSolution(@PathVariable Long solutionId,@PathVariable Integer days) {
        try {
            ProjectSolution solution = solutionService.resubmitSolution(solutionId,days);
            return R.ok("提交成功",solution);
        } catch (Exception e) {
            log.error("提交失败: {}", e.getMessage(), e);
            return R.fail("提交失败: " + e.getMessage());
        }
    }


    /**
     * 更新时常
     */
    @MonitorMethod(bizType = "solutionOverdue", extractor = SolutionDataExtractor.class)
    @Log(title = "更新项目方案时常", linkType = "project", businessType = BusinessType.INSERT, idField = "solutionName", nameField = "solutionName")
    @PostMapping("/submitDays/{solutionId}/{days}")
    public R<ProjectSolution> submitDays(@PathVariable Long solutionId,@PathVariable Integer days) {
        try {
            ProjectSolution solution = solutionService.submitDays(solutionId,days);
            return R.ok("更新项目方案时长成功",solution);
        } catch (Exception e) {
            log.error("更新项目方案时常失败: {}", e.getMessage(), e);
            return R.fail("更新项目方案时长失败: " + e.getMessage());
        }
    }

    /**
     * 审核通过
     */
    @MonitorMethod(bizType = "solutionOverdue", extractor = SolutionDataExtractor.class)
    @Log(title = "项目方案审批-通过", linkType = "project", businessType = BusinessType.INSERT, idField = "solutionName", nameField = "solutionName")
    @PostMapping("/approve/{solutionId}/{auditUser}")
    public R<?> approveSolution(@PathVariable Long solutionId,
                                     @PathVariable String auditUser) {
        try {
            ProjectSolution solution = solutionService.approveSolution(solutionId, auditUser);
            return R.ok("审核通过成功",solution);
        } catch (Exception e) {
            log.error("审核通过失败: {}", e.getMessage(), e);
            return R.fail("审核通过失败: " + e.getMessage());
        }
    }
}
